<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div class="app">
        <input type="text" v-model="keyword">{{keyword}}
        <button @click="sorttype=1">升序</button>
        <button @click="sorttype=2">降序</button>
        <button @click="sorttype=0">原来</button>
        <ul>
            <li v-for="(item,index) in filperson" :key="index"> {{item.name}}-{{item.age}}</li>
        </ul>
    </div>
    <script>
        const vm = new Vue({
            data: {
                sorttype: 0,
                person: [{
                    name: '周杰伦',
                    age: 18
                }, {
                    name: '马冬梅',
                    age: 19
                }, {
                    name: '周冬雨',
                    age: 20
                }, {
                    name: '张鹤伦',
                    age: 21
                }],
                keyword: "",
            },
            // watch: {
            //     keyword: {
            //         immediate: true,
            //         handler(val) {
            //             console.log(val);
            //             this.filperson = this.person.filter((v) => {
            //                 return v.name.indexOf(val) != -1
            //             })
            //         }
            //     }
            // }
            computed: {
                filperson() {
                    const chan = this.person.filter((v) => {
                        return v.name.indexOf(this.keyword) != -1
                    })
                    if (this.sorttype) {
                        chan.sort((a, b) => {
                            return this.sorttype == 1 ? a.age - b.age : b.age - a.age
                        })
                    }
                    return chan
                }
            }
        }).$mount(".app")
    </script>
</body>

</html>